package com.dkd.sbtz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.sbtz.domain.AssetCritical;
import com.dkd.sbtz.service.IAssetCriticalService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 设备关键指标Controller
 * 
 * @author xzj
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/sbtz/critical")
public class AssetCriticalController extends BaseController
{
    @Autowired
    private IAssetCriticalService assetCriticalService;

    /**
     * 查询设备关键指标列表
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetCritical assetCritical)
    {
        startPage();
        List<AssetCritical> list = assetCriticalService.selectAssetCriticalList(assetCritical);
        return getDataTable(list);
    }

    /**
     * 导出设备关键指标列表
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:export')")
    @Log(title = "设备关键指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetCritical assetCritical)
    {
        List<AssetCritical> list = assetCriticalService.selectAssetCriticalList(assetCritical);
        ExcelUtil<AssetCritical> util = new ExcelUtil<AssetCritical>(AssetCritical.class);
        util.exportExcel(response, list, "设备关键指标数据");
    }

    /**
     * 获取设备关键指标详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assetCriticalService.selectAssetCriticalById(id));
    }

    /**
     * 新增设备关键指标
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:add')")
    @Log(title = "设备关键指标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetCritical assetCritical)
    {
        return toAjax(assetCriticalService.insertAssetCritical(assetCritical));
    }



    @PostMapping("/batch")  // ✅ 必须是 @PostMapping
    public AjaxResult addBatch(@RequestBody List<AssetCritical> list) {
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("数据不能为空");
        }
        return toAjax(assetCriticalService.insertBatch(list));
    }


    /**
     * 修改设备关键指标
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:edit')")
    @Log(title = "设备关键指标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetCritical assetCritical)
    {
        return toAjax(assetCriticalService.updateAssetCritical(assetCritical));
    }

    /**
     * 删除设备关键指标
     */
    @PreAuthorize("@ss.hasPermi('sbtz:critical:remove')")
    @Log(title = "设备关键指标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assetCriticalService.deleteAssetCriticalByIds(ids));
    }
}
